
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


////////////////////////////////////////////////////////////////////////////////
//
// Utilities for std::string
// defined in namespace strutil
// by James Fancy
// 
// Modify to miniutil::util and use reference to change itself
//
////////////////////////////////////////////////////////////////////////////////

#ifndef MINIUTIL__UTIL__STRUTIL__H
#define MINIUTIL__UTIL__STRUTIL__H


#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <algorithm>

#include <string.h>

#ifndef WIN32
static inline int strnicmp( const char * src, const char * dst, size_t len )
{
	return strncasecmp( src, dst, len );
}
static inline int stricmp( const char * src, const char * dst )
{
	return strcasecmp( src, dst);
}
#endif

// declaration
namespace miniutil 
{
	namespace strutil
	{
		std::vector<std::string> split(const std::string& str, const std::string& delimiters);
        std::vector<std::string> splitwe(const std::string& str, const std::string& delimiters);
		int convertAsc2Hex(const char* buf_in, char* buf_out, int len); //convert 0x01 to "01"
		void convertHex2Asc(const char* buf_in, char* buf_out, int len);
        std::string convertHex2Asc(const char* buf_in, int len);
		char** parseWinCommandlineToArgv(const char* cmd, int* nargc);
        std::string format(const char * fmt, ... );
        std::string replace(const std::string&, const std::string& from, const std::string& to);


	// Tokenizer class
		class Tokenizer
		{
		public:
			static const std::string DEFAULT_DELIMITERS;
			Tokenizer(const std::string& str);
			Tokenizer(const std::string& str, const std::string& delimiters);

			bool nextToken();
			bool nextToken(const std::string& delimiters);
			const std::string getToken() const;

			/**
			* to reset the tokenizer. After reset it, the tokenizer can get
			* the tokens from the first token.
			*/
			void reset();

		protected:
			size_t m_Offset;
			const std::string m_String;
			std::string m_Token;
			std::string m_Delimiters;
		};

        //-----------------
        using namespace std;

		inline const 
        string toLower( const string& ti) {
            std::string t = ti;
			transform(t.begin(), t.end(), t.begin(), (int(*)(int))tolower );
			return t;
		}

		inline const 
        string toUpper( const string& ti) {
            std::string t = ti;
			transform(t.begin(), t.end(), t.begin(), (int(*)(int))toupper );
			return t;
		}

        inline const
		string trimLeft( const string& ti) {
            std::string t = ti;
			return t.erase(0, t.find_first_not_of(" \t\n\r"));
		}
        inline const
		string trimRight( const string& ti) {
            std::string t = ti;
			return t.erase(t.find_last_not_of(" \t\n\r") + 1);
		}
        inline const
		string trim( const string& ti) {
            std::string t = ti;
			t.erase(0, t.find_first_not_of(" \t\n\r"));
			t.erase(t.find_last_not_of(" \t\n\r") + 1);
			return t;
		}

        inline const
		string trim_left_until( const string& ti, const char * mark_string ) {
            std::string t = ti;
            return t.erase(0, t.find_first_of(mark_string));
		}

        inline const
		string trim_right_until( const string& ti, const char * mark_string) {
            std::string t = ti;
			return t.erase(t.find_last_of(mark_string) + 1);
		}

        inline
		bool startsWith(const string& str, const string& substr) {
			return str.find(substr) == 0;
		}

        inline
		bool endsWith(const string& str, const string& substr) {
			return str.rfind(substr) == (str.length() - substr.length());
		}

		template<class T>
        inline 
        T parseString(const std::string& str) {
			T value;
			std::istringstream iss(str);
			iss >> value;
			return value;
		}

		inline 
        bool parseBool(const std::string& str) {
            return parseString<bool>(str );
		}

        inline int  parseInt( const std::string & str ) {
            return parseString<int>(str);
        }

		template<class T> 
        inline
        T parseHexString(const std::string& str) {
			T value;
			std::istringstream iss(str);
            iss >> std::hex >> value;
			return value;
		}

        inline int  parseHexInt( const std::string & str ) {
            return parseHexString<int>(str);
        }

        bool equalsIgnoreCase(const std::string& str1, const std::string& str2);
 
        template<class T>
        inline 
        std::string toString(const T& value) {
			std::ostringstream oss;
			oss << value;
			return oss.str();
		}

        inline	
        std::string toString(const bool& value) {
            return value ? "true" : "false";
        }

		//string toString(const bool& value) {
		//	ostringstream oss;
		//	oss << boolalpha << value;
		//	return oss.str();
		//}
	}
}

//!!deprecate, move to strutil....
namespace miniutil {
	namespace util
	{
        using namespace std;

		inline const 
        string & toLower( string& t) {
			transform(t.begin(), t.end(), t.begin(), (int(*)(int))tolower );
			return t;
		}

		inline const 
        string & toUpper( string& t) {
			transform(t.begin(), t.end(), t.begin(), (int(*)(int))toupper );
			return t;
		}

        inline const
		string & trimLeft( string& t) {
			return t.erase(0, t.find_first_not_of(" \t\n\r"));
		}
        inline const
		string & trimRight( string& t) {
			return t.erase(t.find_last_not_of(" \t\n\r") + 1);
		}
        inline const
		string & trim( string& t) {
			t.erase(0, t.find_first_not_of(" \t\n\r"));
			t.erase(t.find_last_not_of(" \t\n\r") + 1);
			return t;
		}

        inline const
		string & trim_left_until( string& t, const char * mark_string ) {
            return t.erase(0, t.find_first_of(mark_string));
		}

        inline const
		string & trim_right_until( string& t, const char * mark_string) {
			return t.erase(t.find_last_of(mark_string) + 1);
		}

        inline
		bool startsWith(const string& str, const string& substr) {
			return str.find(substr) == 0;
		}

        inline
		bool endsWith(const string& str, const string& substr) {
			return str.rfind(substr) == (str.length() - substr.length());
		}

		template<class T>
        inline 
        T parseString(const std::string& str) {
			T value;
			std::istringstream iss(str);
			iss >> value;
			return value;
		}

		inline 
        bool parseBool(const std::string& str) {
            return parseString<bool>(str );
		}

        inline int  parseInt( const std::string & str ) {
            return parseString<int>(str);
        }

		template<class T> 
        inline
        T parseHexString(const std::string& str) {
			T value;
			std::istringstream iss(str);
            iss >> std::hex >> value;
			return value;
		}

        inline int  parseHexInt( const std::string & str ) {
            return parseHexString<int>(str);
        }

        bool equalsIgnoreCase(const std::string& str1, const std::string& str2);
 
        template<class T>
        inline 
        std::string toString(const T& value) {
			std::ostringstream oss;
			oss << value;
			return oss.str();
		}

        inline	
        std::string toString(const bool& value) {
            return value ? "true" : "false";
        }

		//string toString(const bool& value) {
		//	ostringstream oss;
		//	oss << boolalpha << value;
		//	return oss.str();
		//}
        
    }
}


#endif
